import axios from 'axios'
import { tansParams } from "@/utils/ruoyi";
import { Message } from 'element-ui'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const client = axios.create({
  // 超时
  timeout: 10000
})

// request拦截器
client.interceptors.request.use(config => {
  // get请求映射params参数
  if (config.method === 'get' && config.params) {
    let url = config.url + '?' + tansParams(config.params);
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config
}, error => {
    console.log(error)
    Promise.reject(error)
})

// 响应拦截器
client.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.code;
    // 获取错误信息
    const msg = res.resultMessage
    if (code !== 0) {
      Message({ message: msg, type: 'error' })
      return Promise.reject(new Error(msg))
    } else {
      return res.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)

// 分类品牌列表
export function getCategoryAndBrandList(gaeaCategoryId) {
  const url = 'https://dubai.aihuishou.com/ahs-yanxuan-service/products/category-and-brand'
  const params = {
    scene: 'YANXUAN_HOME',
    gaeaCategoryId: gaeaCategoryId
  }
  return client({
    url: url,
    method: 'get',
    params: params
  })
}

export default client
